/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.attendance.clazz.service.impl;

import com.arisgo.cloud.attendance.clazz.entity.WorkSystemDetail;
import com.arisgo.cloud.attendance.clazz.mapper.WorkSystemDetailMapper;
import com.arisgo.cloud.attendance.clazz.vo.WorkSystemDetailVo;
import com.arisgo.cloud.common.utils.AssertUtil;
import com.arisgo.cloud.web.mybatis.service.CloudServiceImpl;
import com.arisgo.cloud.attendance.clazz.mapper.WorkSystemMapper;
import com.arisgo.cloud.attendance.clazz.service.WorkSystemService;
import com.arisgo.cloud.attendance.clazz.entity.WorkSystem;
import com.arisgo.cloud.attendance.clazz.vo.WorkSystemVo;
import com.arisgo.cloud.web.utils.RequestUtil;
import com.google.common.collect.Lists;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作制度
 *
 * @author Coulson
 * @since 2025-04-30
 */
@Service
@AllArgsConstructor
public class WorkSystemServiceImpl extends CloudServiceImpl<WorkSystemMapper, WorkSystem, WorkSystemVo> implements WorkSystemService {

    @Override
    public WorkSystem getInstanceModel() {
        return new WorkSystem();
    }

    @Override
    public WorkSystemVo getInstanceVo() {
        return new WorkSystemVo();
    }

    private final WorkSystemDetailMapper workSystemDetailMapper;



    @Override
    @Transactional
    public boolean insertWorkSystemVo(WorkSystemVo vo) {
        AssertUtil.notNull(vo, "参数为空！");
        // 保存工制
        WorkSystem workSystem = new WorkSystem();
        BeanUtils.copyProperties(vo, workSystem);
        workSystem.setTenantId(RequestUtil.getTenantId());
        this.mapper.insert(workSystem);

        saveWorkSystemDetail(workSystem.getId(), vo.getDetail());
        return true;
    }

    @Override
    @Transactional
    public boolean updateWorkSystemVo(WorkSystemVo vo) {
        AssertUtil.notNull(vo, "参数为空！");

        // 保存工制
        WorkSystem workSystem = new WorkSystem();
        BeanUtils.copyProperties(vo, workSystem);
        workSystem.setTenantId(RequestUtil.getTenantId());
        this.mapper.update(workSystem);

        // 删除历史详情
        Map<String, Object> params = new HashMap<>();
        params.put(WorkSystemDetail.TENANT_ID, RequestUtil.getTenantId());
        params.put(WorkSystemDetail.SYSTEM_ID, workSystem.getId());
        workSystemDetailMapper.deleteByMap(params);

        saveWorkSystemDetail(workSystem.getId(), vo.getDetail());
        return true;
    }

    private void saveWorkSystemDetail(Long systemId, List<WorkSystemDetailVo> vos) {
        if (CollectionUtils.isEmpty(vos)) {
            return;
        }

        // 保存工制详情
        List<WorkSystemDetail> detail = Lists.newArrayListWithCapacity(vos.size());
        for (WorkSystemDetailVo vo : vos) {
            WorkSystemDetail entity = new WorkSystemDetail();
            BeanUtils.copyProperties(vo, entity);
            entity.setSystemId(systemId);
            entity.setTenantId(RequestUtil.getTenantId());
            detail.add(entity);
        }
        workSystemDetailMapper.insertBatch(detail);
    }

    @Override
    @Transactional
    public boolean removeWorkSystem(Long id) {
        AssertUtil.notNull(id, "参数为空！");
        // 删除详情
        Map<String, Object> params = new HashMap<>();
        params.put(WorkSystemDetail.TENANT_ID, RequestUtil.getTenantId());
        params.put(WorkSystemDetail.SYSTEM_ID, id);
        workSystemDetailMapper.deleteByMap(params);

        this.mapper.deleteById(id);
        return true;
    }

    @Override
    @Transactional
    public boolean batchRemoveWorkSystem(List<Long> ids) {
        AssertUtil.notEmpty(ids, "参数为空！");

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(WorkSystemDetail.TENANT_ID, RequestUtil.getTenantId());
        wrapper.in(WorkSystemDetail.SYSTEM_ID, ids);
        workSystemDetailMapper.deleteByQuery(wrapper);

        this.mapper.deleteBatchByIds(ids);
        return true;
    }
}
